//index.js
//获取应用实例
var app = getApp();
let httpUrl = app.globalData.httpUrl;
let thisErpId = app.globalData.thisErpId;
Page({
  data: {
    mode: "scaleToFill",
    winHeight: 0,
    imgArr: [],
    indicatorDots: false,
    autoplay: true,
    interval: 2000,
    duration: 1000,
    //排行版数据
    materialData: [],
    page: 1,
    searchPageType: 1,
    searchType: '荒料',
    unitText: '体积',
    rightUnit: 'm³',
    //是否还能加载
    isLoad: true,
    stayTitle: false,
    second_height: 200,
    isLoadRequest: true,
    //是否允许下拉刷新
    isUpper: true,

    isAndroid: false,
  },
  /**
 * 生命周期函数--监听页面加载
 */
  onLoad: function (options) {
    // let searchPageType = options.searchPageType || '1';
    // let searchType = (searchPageType == '1') ? '荒料' : '大板';
    // let unitText = (searchPageType == '1') ? '体积' : '面积';
    // let rightUnit = (searchPageType == '1') ? 'm³' : 'm²';
    // this.setData({
    //   searchPageType: searchPageType,
    //   searchType: searchType,
    //   unitText: unitText,
    //   rightUnit: rightUnit,
    // })

    wx.getSystemInfo({
      success: (res) => {
        if (res.windowWidth) {
          this.setData({
            // second部分高度 = 利用窗口可使用高度 - first部分高度（这里的高度单位为px，所有利用比例将400rpx转换为px）
            second_height: (res.windowWidth / 750) * 400,
            winHeight: res.windowHeight,
          })
        }
        //判断手机操作系统
        let system = res.system;
        console.log(system)
        if (system.indexOf('Android') > -1) {
          this.setData({
            isAndroid: true,
          })
        }
      }
    })

    // 排行版获取
    this.getMaterial();
    // 轮播图获取
    this.getSliderImg();
  },
  /**
* 获取排行榜
*/
  getMaterial() {
    let isLoadRequest = this.data.isLoadRequest;
    if (isLoadRequest) {
      this.setData({
        isLoadRequest: false,
      })
      const page = this.data.page;
      let WLTYPE = this.data.searchPageType;
      WLTYPE = (WLTYPE == 1) ? 'huangliao' : 'daban';
      if (wx.showLoading) {
        wx.showLoading({
          title: '正在加载...',
        })
      }
      wx.request({
        url: httpUrl + 'slsw/mtloutrank.do',
        method: 'POST',
        data: `weChatKey=9237B361957490BA9644A710A5E3DD84&&Data={
        is_refresh:'${page == 1}',page_num:${page},item_num:20,wltype:'${WLTYPE}',erpId:'${thisErpId}'}`,
        header: {
          'content-type': 'application/x-www-form-urlencoded;charset=UTF-8"'
        },
        success: (res) => {
          const list = this.formatNum(res.data.Data);
          let blockIsLoad = (list.length == 20) ? true : false;
          this.setData({
            materialData: page == 1 ? list : [...this.data.materialData, ...list],
            blockIsLoad: blockIsLoad,
            isLoadRequest: true,
          })
          wx.hideLoading();
        },
        fail: () => {
          wx.hideLoading();
          wx.showToast({
            title: '获取排行榜失败，请稍后再试!',
            icon: 'loading'
          })
          this.setData({
            isLoadRequest: true,
          })
        }
      })
    }
  },
  /**
* 获取轮播图
*/
  getSliderImg: function () {
    wx.request({
      url: httpUrl + 'slsw/sliderimgs.do',
      method: 'POST',
      data: `weChatKey=9237B361957490BA9644A710A5E3DD84&&Data={erpId:'${thisErpId}'}`,
      header: {
        'content-type': 'application/x-www-form-urlencoded;charset=UTF-8"'
      },
      success: (res) => {
        this.setData({
          imgArr: res.data.Data,
        })
      },
      fail: () => {
        wx.hideLoading();
        wx.showToast({
          title: '获取banner图失败，请稍后再试!',
          icon: 'loading'
        })
      }

    })
  },
  entrancebm: function (e) {
    wx.navigateTo({
      url: '../bmindex/bmindex',
    })
  },
  entrancesl: function (e) {
    wx.navigateTo({
      url: '../demo/demo',
    })
  },
  // onReachBottom
  // onReachBottom: function (e) {
  //   let blockIsLoad = this.data.blockIsLoad;
  //   if (blockIsLoad == true) {
  //     this.setData({
  //       page: this.data.page + 1
  //     })
  //     this.getMaterial();
  //   } else {
  //     wx.hideLoading();
  //     wx.showToast({
  //       title: '已经没有更多数据了!' ,
  //       icon: 'clear'
  //     })
  //   }
  // },
  onScroll: function (e) {
    let isLoadRequest = this.data.isLoadRequest;
    if (isLoadRequest) {
      let blockIsLoad = this.data.blockIsLoad;
      if (blockIsLoad == true) {
        this.setData({
          page: this.data.page + 1
        })
        this.getMaterial();
      } else {
        wx.hideLoading();
        wx.showToast({
          title: '已经没有更多数据了!',
          icon: 'clear'
        })
      }
    }
  },
  /** 
     * 搜索页按钮 
     */
  searchPageBtn: function (e) {
    let searchType = this.data.searchPageType;
    wx.navigateTo({
      url: '../search-sort/search-sort?searchPageType=' + searchType,
    })
  },
  /** 
  * 列表页详情按钮 
  */
  stoneDetails: function (e) {
    console.log(e)
    wx.navigateTo({
      url: '../stone-details/stone-details?id=111',
    })
  },
  getScrollHeight: function (e) {
    let scrollTop = e.detail.scrollTop;
    let second_height = this.data.second_height;
    if (scrollTop > (second_height - 1)) {
      this.setData({
        stayTitle: true,
      })
    }
    if (scrollTop < (second_height + 1)) {
      this.setData({
        stayTitle: false,
      })
    }
  },
  /**
* 格式化number保留三个小数点
*/
  formatNum: function (arr) {
    for (let i = 0; i < arr.length; i++) {
      arr[i].VAQty = (arr[i].VAQty).toFixed(3);
    }
    return arr;
  },
  /**
* 下拉刷新
*/
  upperData: function () {
    let isLoadRequest = this.data.isLoadRequest;
    let isUpper = this.data.isUpper;
    let that = this;
    if (isLoadRequest && isUpper) {
      if (wx.startPullDownRefresh) {
        wx.startPullDownRefresh({
          success: function () { }
        })
      }
      // setTimeout(function () {
      that.setData({
        page: 1,
        isUpper: false
      })
      // 如果时Android执行隐藏动画
      if (that.data.isAndroid){
        wx.stopPullDownRefresh()
      }
      that.getMaterial();
      // }, 1500)
    } else {
      // that.setData({
      //   isUpper: false
      // })
    }
  },
  touchUp: function () {
    this.setData({
      isUpper: true,
    })
  },
  // onPullDownRefresh: function () {
  //   let that = this;
  //   setTimeout(function () {
  //     that.setData({
  //       page: 1,
  //     })
  //     that.loadData();
  //   }, 1500)
  // },
  /**
 * 用户点击右上角分享
 */
  onShareAppMessage: function () {

  }
  // entrancesl: function (e) { 
  //   wx.showToast({
  //     title: '暂未开放!',
  //     icon: 'clear'
  //   })
  // },

})
